﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using OrganizedIN.DAL;
using OrganizedIN.BAL;
using OrganizedIN.BussinessServices;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
namespace OrganizedIN
{
    public partial class Home : System.Web.UI.Page
    {
        DataAccessLayer dl = new DataAccessLayer();
        public static int log = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                log = 0;
            }
          txtusername.Focus();  
        }
        protected void btnlogin_Click(object sender, EventArgs e)
        {
            try
            {
                BLL loginobj = new BLL();
                User_Login setdataobj = new User_Login();
                ArrayList getdata = new ArrayList();
                SqlService objservices = new SqlService();

                DataSet dsAccount = loginobj.AccountLocked_Fetch(txtusername.Text.Trim());
                if (dsAccount.Tables[0].Rows.Count > 0)
                {
                    DateTime dtlock = Convert.ToDateTime(dsAccount.Tables[0].Rows[0]["LockedTime"].ToString()).AddHours(6);
 
                    DateTime dtNow = System.DateTime.Now;
                    if (dtNow >= dtlock)
                    {
                        string Qry = "Delete from User_AccountLocked where Username='" + txtusername.Text.Trim() + "'";
                        SqlHelper.ExecuteNonQuery(objservices.ConnectionString, CommandType.Text, Qry);

                        string qry1 = "Update userinformation set Status='1' where Username='" + txtusername.Text.Trim() + "'";
                        SqlHelper.ExecuteNonQuery(objservices.ConnectionString, CommandType.Text, qry1);
                    }
                    
                }

                setdataobj.UserName = txtusername.Text.Trim();
                setdataobj.UserPassword = txtpassword.Text.Trim();
                getdata = loginobj.UserLogin(setdataobj);
                
                if (getdata.Count != 0)
                {
                    Session["UserID"] = setdataobj.UserName;
                    Session["FName"] = Convert.ToString(getdata[0]);
                    Session["RegisDate"] = Convert.ToString(getdata[1]);
                    Session["UserName"] = Convert.ToString(getdata[0]);
                    string status="";
                    try
                    {
                        status = getdata[2].ToString();
                    }
                    catch (Exception ex)
                    {
                    }
                    if (status == "1")
                    {
                        Response.Redirect(@"\UI\ToDo\MyToDos.aspx");
                    }
                    else
                    {
                        Response.Redirect(@"\UI\Authentiction.aspx?id=" + setdataobj.UserName);
                    }
                }
                else
                {
                    if (log < 2)
                    {
                        lblErrorMessage.Text = "Invalid username and password.";
                        log++;
                    }
                    else
                    {
                        Session["UserID"] = setdataobj.UserName;
                        Response.Redirect("~/UI/AccountLockedMsg.aspx");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void btnsignup_Click(object sender, EventArgs e)
        {
            try
            {
                String url = string.Empty;
                if (radbtnbusiness.Checked)
                {
                    url = "/UI/GroupRegistration.aspx";
                    ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>OpenRegisPopup1('" + url + "')</script>");

                }
                else
                {
                    url = "/UI/registration.aspx";
                    ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>OpenRegisPopup('" + url + "')</script>");

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
     }
 }
    

